# smtplib 用于邮件的发信动作
import smtplib
from loguru import logger
# email 用于构建邮件内容
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
# 构建邮件头
from email.header import Header
from email.mime.application import MIMEApplication

class sendEmail:
  
  def __init__(self,maillist:list,mailtitle:str,mailcontent) -> None:
     self.mail_list = maillist
     self.mail_title = mailtitle
     self.mail_content = mailcontent
     
     
     self.mail_host = "smtp.163.com"
     self.mail_user = "your email name"
     self.mail_pass = "your email password"
     self.mail_postfix = "163.com"
  
  
  def sendMail(self,Subject,file):
    me = self.mail_user + "<" + self.mail_user + "@" + self.mail_postfix + ">"
    msg = MIMEMultipart()
    msg['Subject'] = Subject
    msg ['From'] = me
    msg['To'] = ";".join()(self.mail_list)
    
    xlsxpart = MIMEApplication(open(file, 'rb').read())
    xlsxpart.add_header('Content-Disposition', 'attachment', filename=file)
    msg.attach(xlsxpart)
    
    try:
      s = smtplib.SMTP() #创建邮件服务器对象
      s.connect(self.mail_host) #连接到指定的smtp服务器。参数分别表示smpt主机和端口
      s.login(self.mail_user, self.mail_pass) #登录到你邮箱
      logger.info("登录邮箱成功")
      s.sendmail(me, self.mail_list, msg.as_string()) #发送内容
      s.close()
      return True
    except Exception as e:
      return False